package compoundDB.gui;

import java.util.ArrayList;

import javax.swing.table.AbstractTableModel;

@SuppressWarnings("serial")
public class NamesTableModel extends AbstractTableModel
{
	private ArrayList<String> m_data;
	private String m_name;
	private NamesTableHolder m_table;
	private boolean m_editable;

	public NamesTableModel(String[] data, NamesTableHolder table, boolean editable)
	{
		m_name = "Other Compound Names";

		this.m_data = new ArrayList<String>();
		for (int i = 0; i < data.length; i++)
		{
			this.m_data.add(data[i]);
		}
		this.m_table = table;
		this.m_editable = editable;
	}

	public String[] dumpToArray()
	{
		return m_data.toArray(new String[0]);
	}

	public String getColumnName(int i)
	{
		return m_name;
	}

	@Override
	public int getColumnCount()
	{
		return 1;
	}

	@Override
	public int getRowCount()
	{
		return m_data.size() + 1;
	}

	public boolean isCellEditable(int rowIndex, int columnIndex)
	{
		return m_editable;
	}

	@Override
	public Object getValueAt(int arg0, int arg1)
	{
		if (arg0 == m_data.size())
		{
			return "";
		}

		return m_data.get(arg0);
	}

	public void setValueAt(Object aValue, int rowIndex, int columnIndex)
	{
		if (rowIndex == m_data.size())
		{
			if (!aValue.equals(""))
			{

				m_data.add(aValue.toString());
				this.fireTableDataChanged();
				m_table.refreshNamesHeight();
			}
			else
			{
				return;
			}
		}
		else
		{
			if (aValue.equals(""))
			{
				m_data.remove(rowIndex);
				m_table.refreshNamesHeight();
			}
			else
			{
				m_data.set(rowIndex, aValue.toString());
			}
			this.fireTableDataChanged();
		}
	}
}
